'''
Created on 15/10/2009

@author: Tony Allan
'''
import unittest
from services import *
from services.httpserver import *       # FIXME: not sure why I need this
from runtime.boot import LoggingLevels

class DrivingTask(Task):
    ''' a user of transformtask and httptask '''
    def cb_start(self, method, uri, res, result, context, **kwargs):
        #self.log('DrivingTask(cb_start): %s' % uri)
        transform_uri = self.context.uri('transform:').template.dump.collection()
        
        # create some test data
        data = Resource()
        data.set_type(context.uri('services:resource/transformtestdata'))
        data.hello = 'World!'
        data.test = ['aaa', {'bbb': ['ccc', 'ddd'], 'eee': 'fff', 'ggg': 'hhh'}, 'iii']
        generated_uri = self.context.put(transform_uri, data)   # resource is transformed when this returns
        gen_res = self.context.remove(generated_uri)            # get then delete the generated html
        self.log('DrivingTask(cb_start): Content=%s' % gen_res.content)
        # setup sample data for the web server at http://localhost:8000/sample/sample1.html
        url = context.uri('local:').sample.add('sample1.html')
        self.log('Sample: %s' % url.full())
        context.put(url, gen_res)
        # add a new HTTP site
        res = Resource()
        http_uri = self.context.uri('tasks:').base.service.httptask
        server_uri = self.context.uri('local:')
        res.listen = server_uri
        self.log('Adding site %s' % res.listen)
        self.context.put(http_uri.listener.local, res)
        # now process HTTP requests until someone else causes us to finish
        # Note: In this example the same resource is returned over and over again 
        # because HTML is only generated once. Not sure the best way to fix this
        # other than process or process_all.

    def cb_stop(self, method, uri, return_value, result, context, **kwargs):
        for u in context.dump():
            print(str(u) + ' = ' + str(context.get(u)))

class TransformTaskTest(unittest.TestCase):

    def test_html_and_http(self):
        '''[htmltask] Test htmltask using httptask. '''
        namespaces = {
            'yarra': 'http://yarra-project.com/terms/yarra/',
            'datastore': 'http://yarra-project.com/terms/base/datastore/',
            'package': 'http://yarra-project.com/terms/base/package/',
            'runtime': 'http://yarra-project.com/terms/base/runtime/',
            'services': 'http://yarra-project.com/terms/base/services/',
            'dc': 'http://purl.org/dc/elements/1.1/',
            'eplanning': 'http://dpcd.vic.gov.au/terms/',
            'spear': 'http://dpcd.vic.gov.au/terms/eplanning/spear/',
            'local': 'http://localhost:8000/',
            'config': 'http://localhost:8000/configuration/',
            'tasks': 'http://localhost:8000/tasks/',
            'pvt': 'http://localhost:8000/spear-pvt/',
            'transform': 'http://localhost:8000/transform/',
            'a2a': 'http://localhost:8000/spear-a2a/',
            'time': 'http://localhost:8000/time/',
            'files': 'http://localhost:8000/files/',
            }
        # setup for tests
        spaces = Spaces()
        queue = Queue()
        context = SpacesContext(spaces, queue)
        context.load_bindings(namespaces)
        #
        # start the html task
        taskh_uri = context.uri('tasks:').base.service.transformtask
        taskh = TransformTask(spaces, taskh_uri)
        taskh.task_start()
        #
        # start the http task
        taska_uri = context.uri('tasks:').base.service.httptask
        taska = HTTPTask(spaces, taska_uri)
        taska.task_start()
        #
        # start the test driver
        time.sleep(0)
        taskd_uri = context.uri('tasks:').base.service.htmltasktest
        taskd = DrivingTask(spaces, taskd_uri)
        taskd.task_start()
        #
        time.sleep(10)
        #
        taskh.task_stop()
        taska.task_stop()
        taskd.task_stop()
        time.sleep(1)


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()